package org.jeecg.modules.health_records.health_record.controller;

import java.sql.Types;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.health_records.health_record.entity.HealthRecordInfo;
import org.jeecg.modules.health_records.health_record.service.IHealthRecordInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;


import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 个人人口学基本信息表
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Api(tags="个人人口学基本信息表")
@RestController
@RequestMapping("/health_records/HealthRecord/healthRecordInfo")
@Slf4j
public class HealthRecordInfoController extends JeecgController<HealthRecordInfo, IHealthRecordInfoService> {
	@Autowired
	private IHealthRecordInfoService healthRecordInfoService;

	/**
	 * 分页列表查询
	 *
	 * @param healthRecordInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "个人人口学基本信息表-分页列表查询")
	@ApiOperation(value="个人人口学基本信息表-分页列表查询", notes="个人人口学基本信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<HealthRecordInfo>> queryPageList(HealthRecordInfo healthRecordInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HealthRecordInfo> queryWrapper = QueryGenerator.initQueryWrapper(healthRecordInfo, req.getParameterMap());
		Page<HealthRecordInfo> page = new Page<HealthRecordInfo>(pageNo, pageSize);
		IPage<HealthRecordInfo> pageList = healthRecordInfoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param healthRecordInfo
	 * @return
	 */
	@AutoLog(value = "个人人口学基本信息表-添加")
	@ApiOperation(value="个人人口学基本信息表-添加", notes="个人人口学基本信息表-添加")
	//@RequiresPermissions("HealthRecord:health_record_demology:add")
	@PostMapping(value = "/add")
	public Result<HealthRecordInfo> add(@RequestBody HealthRecordInfo healthRecordInfo) {
		Long seq = getNextHealthSequence("healthSequence");
		healthRecordInfo.setRecordid(seq);
		healthRecordInfoService.save(healthRecordInfo);
		return Result.OK("添加成功！",healthRecordInfo);

	}
	
	/**
	 *  编辑
	 *
	 * @param healthRecordInfo
	 * @return
	 */
	@AutoLog(value = "个人人口学基本信息表-编辑")
	@ApiOperation(value="个人人口学基本信息表-编辑", notes="个人人口学基本信息表-编辑")
	//@RequiresPermissions("HealthRecord:health_record_demology:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody HealthRecordInfo healthRecordInfo) {
		healthRecordInfoService.updateById(healthRecordInfo);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "个人人口学基本信息表-通过id删除")
	@ApiOperation(value="个人人口学基本信息表-通过id删除", notes="个人人口学基本信息表-通过id删除")
	//@RequiresPermissions("HealthRecord:health_record_demology:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		healthRecordInfoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "个人人口学基本信息表-批量删除")
	@ApiOperation(value="个人人口学基本信息表-批量删除", notes="个人人口学基本信息表-批量删除")
	//@RequiresPermissions("HealthRecord:health_record_demology:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.healthRecordInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "个人人口学基本信息表-通过id查询")
	@ApiOperation(value="个人人口学基本信息表-通过id查询", notes="个人人口学基本信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<HealthRecordInfo> queryById(@RequestParam(name="id",required=true) String id) {
		HealthRecordInfo healthRecordInfo = healthRecordInfoService.getById(id);
		if(healthRecordInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(healthRecordInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param healthRecordInfo
    */
    //@RequiresPermissions("HealthRecord:health_record_demology:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HealthRecordInfo healthRecordInfo) {
        return super.exportXls(request, healthRecordInfo, HealthRecordInfo.class, "个人人口学基本信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("HealthRecord:health_record_demology:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HealthRecordInfo.class);

	}
	 @Autowired
	 private JdbcTemplate jdbcTemplate;
	 /**
	  *  使用JdbcTemplate调用存储过程获取健康档案编号 17位数字
	  *
	  * @param sequenceName
	  * @return
	  */
	 @ApiOperation(value="获取健康档案编号17位数字", notes="传入参数为数据库序列名，如健康档案编号17位数字的数据库序列名称为healthSequence")
	 @GetMapping(value = "/getNextHealthSequence")
	 public Long getNextHealthSequence(String sequenceName) {
		 SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
				 .withProcedureName("get_next_sequence_value")
				 .declareParameters(
						 new SqlParameter("seqName", Types.VARCHAR),
						 new SqlOutParameter("seqValue", Types.BIGINT)
				 );
         // 准备输入参数
		 Map<String, Object> inParams = new HashMap<>();
		 inParams.put("seqName", sequenceName);
		 Map<String, Object> result = jdbcCall.execute(inParams);
		 return (Long) result.get("seqValue");
	 }


	 @ApiOperation(value="个人人口学基本信息表-按recordid查询", notes="个人人口学基本信息表-按recordid查询")
	 @PostMapping(value = "/queryByRecordId")
	 public  Result<List<HealthRecordInfo>> queryByRecordId(@RequestBody HealthRecordInfo healthRecordInfo) {
		 //判断查询参数是否为空
		 if((healthRecordInfo.getRecordid()==null||"".equals(healthRecordInfo.getRecordid()))
				 &&
				 (healthRecordInfo.getIdNumber()==null||"".equals(healthRecordInfo.getIdNumber()))
				 &&
				 (healthRecordInfo.getName()==null||"".equals(healthRecordInfo.getName()))) {

			 return Result.error("请输入查询参数：recordID或身份证号码或姓名查询");
		 }if(healthRecordInfo.getRecordid()!=null&&healthRecordInfo.getRecordid()==0&&
				 (healthRecordInfo.getIdNumber()==null||"".equals(healthRecordInfo.getIdNumber()))
				 &&
				 (healthRecordInfo.getName()==null||"".equals(healthRecordInfo.getName()))){
			 return Result.error("请输入查询参数：recordID或身份证号码或姓名查询");
		 } else {

			 List<HealthRecordInfo> list1 = healthRecordInfoService.queryByRecordId(healthRecordInfo);
			 return Result.OK(list1);
		 }
	 }

 }
